import { inject } from "vue"
import { ProvideRootProps } from "../keys"
import { getValueByModel } from "../util"

export const useCheck = function (props: CellProps){
    const RootProps = inject(ProvideRootProps) as Props


    function checkReadonly():boolean{
        if (RootProps.readonly === true) return true
        if (props.column.readonly === true) return true

        if (typeof RootProps.readonly === 'function') {
            return RootProps.readonly({ 
                dataIndex: props.column.dataIndex, 
                text: getValueByModel(props.column.dataIndex, props.record),
                column: props.column,
                record: props.record,
            })
        }
        if (typeof props.column.readonly === 'function') {
            return props.column.readonly({ 
                dataIndex: props.column.dataIndex, 
                text: getValueByModel(props.column.dataIndex, props.record),
                column: props.column,
                record: props.record,
            })
        }

        return false
    }
    function checkDisabled():boolean{
        if (RootProps.disabled === true) return true
        if (props.column.disabled === true) return true

        if (typeof RootProps.disabled === 'function') {
            return RootProps.disabled({ 
                dataIndex: props.column.dataIndex, 
                text: getValueByModel(props.column.dataIndex, props.record),
                column: props.column,
                record: props.record,
            })
        }
        if (typeof props.column.disabled === 'function') {
            return props.column.disabled({ 
                dataIndex: props.column.dataIndex, 
                text: getValueByModel(props.column.dataIndex, props.record),
                column: props.column,
                record: props.record,
            })
        }

        return false
    }

    return { checkReadonly, checkDisabled }
}

